int speakerPin = 8;
int ledPin = 13;

// amount of chirps per annoy-o-beep
int chirps = 2;

// minimum and maximum delay between annoy-o-beeps in seconds
int pauseMin = 45;
int pauseMax = 300;

// delay (in seconds) after first test beep before beginning normal operation
// this is so you can place the device and create an alibi ;)
int firstRunDelay = 300;
int firstRun = 0; // flag

// still working out how this works
// tone is the actual tone that is created by the piezo, lower numbers
// are higher tones (??), variable is in microseconds
// duration is the length of the tone in milliseconds
int tone = 230;
int duration = 100;

long dlen = 0;

void chirp()
{
  digitalWrite(ledPin, HIGH);
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
  digitalWrite(ledPin, LOW);
  delay(duration / 10);
}

void setup()
{
  pinMode(speakerPin, OUTPUT);
  randomSeed(analogRead(5));
  Serial.begin(9600);
}

void loop()
{
  if (!firstRun)
  {
    Serial.print("Waiting for device placement...");
    delay(firstRunDelay * 1000L);
    firstRun = 1;
    Serial.println("Done. Resuming normal operation.");
  }
  else
  {
    Serial.println("Beep!");
    for (int i=0;i<chirps;i++) chirp();
    dlen = random(pauseMin, pauseMax+1L);
    Serial.print("Waiting ");
    Serial.print(dlen, DEC);
    Serial.println(" seconds...");
    delay(dlen * 1000L);
  }
}

